package com.wsm.common.business.impl;

import com.wsm.common.business.ConfigBusiness;
import com.wsm.common.domain.ConfigDO;
import com.wsm.common.enums.BaseEnum;
import com.wsm.common.service.ConfigService;
import com.wsm.common.utils.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 系统配置信息表
 *
 * @author wangshimin
 * @email wangshiminhome@foxmail.com
 * @date 2020-02-14 17:43:18
 * @description : 系统配置信息表
 */
@Service
public class ConfigBusinessImpl implements ConfigBusiness {
	@Autowired
	private ConfigService configService;

	@Override
	public ConfigDO get(Long id) {
		return configService.getById(id);
	}

	@Override
	public Result<PageUtils> list(Map<String, Object> params) {
		//查询列表数据
		Query query = new Query(params);
		List<ConfigDO> configList = configService.list(query);
		int total = configService.count(query);
		PageUtils pageUtils = new PageUtils(configList, total);
		return Result.success(pageUtils);
	}

	@Override
	public Result save(ConfigDO config) {
		config.setId(SnowFlake.nextId());
		config.setUseFlag(BaseEnum.USE_FLAG.YES.getCode());
		config.setCreateId(ShiroUtils.getUserId());
		config.setCreateTime(new Date());
		if (configService.orSave(config) > 0) {
			return Result.success();
		}
		return Result.error();
	}

	@Override
	public Result update(ConfigDO config) {
		configService.update(config);
		return Result.success();
	}

	@Override
	public Result remove(Long id) {
		if (configService.remove(id) > 0) {
			return Result.success();
		}
		return Result.error();
	}

	@Override
	public Result batchRemove(Long[] ids) {
		configService.batchRemove(ids);
		return Result.success();
	}


	public Result updateUseFlag(Long id, Integer useFlag) {
		if (configService.updateUseFlag(id, useFlag) > 0) {
			return Result.success();
		}
		return Result.error();
	}

}
